package com.fanqwz.kotlinmvvm.ui.fragment

import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.annotation.CallSuper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.fanqwz.kotlinmvvm.databinding.AppDataBindingComponent
import com.fanqwz.kotlinmvvm.presenter.viewmodel.IModel
import com.fanqwz.kotlinmvvm.presenter.viewmodel.IViewModel
import com.fanqwz.kotlinmvvm.ui.compositor.ViewCompositor
import com.fanqwz.kotlinmvvm.view.IView
import com.fanqwz.kotlinmvvm.view.MVVM
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import javax.inject.Inject

/**
 * Created by fanqwz on 2017/11/2.
 */

abstract class MVVMFragment<V : IView, M : IModel, VM : IViewModel<M>> : BaseFragment() {
    val logger: Logger by lazy { LoggerFactory.getLogger(this::class.simpleName) }
    @Inject
    protected lateinit var mvvm: MVVM<V, M, VM>
    @Inject
    lateinit var dataBindingComponent: AppDataBindingComponent

    @CallSuper
    open fun buildViewCompositor(): ViewCompositor {
//        return viewCompositor
        return mvvm.view.viewCompositor
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        logger.trace("onCreate")
    }

    final override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        logger.trace("onCreateView")
        buildViewCompositor()
//        return viewCompositor.buildViewLayout(inflater)
        return mvvm.view.viewCompositor.buildViewLayout(layoutInflater)
    }

    override fun onStart() {
        super.onStart()
        logger.trace("onStart")
        mvvm.start()
    }

    override fun onStop() {
        super.onStop()
        logger.trace("onStop")
        mvvm.stop()
    }

    override fun onResume() {
        super.onResume()
        logger.trace("onResume")
    }

    override fun onPause() {
        super.onPause()
        logger.trace("onResume")
    }

    override fun onDestroy() {
        super.onDestroy()
        logger.trace("onDestroy")
        mvvm.destroy()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        logger.trace("onSaveInstanceState")
    }

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
        logger.trace("onViewStateRestored")
    }
}
